home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / programr / atre27.exe / ATREE_27 / OCRDEMO2 / BITMAP.CPP next >
C/C++ Source or Header  |  1992-08-01  |  14KB  |  546 lines

  1. /*****************************************************************************
  2.  ****                                                                     ****
  3.  **** bitmap.cpp                                                          ****
  4.  ****                                                                     ****
  5.  **** atree release 2.7 for Windows                                       ****
  6.  **** Adaptive Logic Network (ALN) simulation program.                    ****
  7.  **** Copyright (C) M. Thomas, N. Sanche, W.W. Armstrong 1991, 1992       ****
  8.  ****                                                                                           ****
  9.  **** License:                                                            ****
  10.  **** A royalty-free license is granted for the use of this software for  ****
  11.  **** NON_COMMERCIAL PURPOSES ONLY. The software may be copied and/or     ****
  12.  **** modified provided this notice appears in its entirety and unchanged ****
  13.  **** in all derived source programs.  Persons modifying the code are     ****
  14.  **** requested to state the date, the changes made and who made them     ****
  15.  **** in the modification history.                                        ****
  16.  ****                                                                     ****
  17.  **** Patent License:                                                     ****
  18.  **** The use of a digital circuit which transmits a signal indicating    ****
  19.  **** heuristic responsibility is protected by U. S. Patent 3,934,231     ****
  20.  **** and others assigned to Dendronic Decisions Limited of Edmonton,     ****
  21.  **** W. W. Armstrong, President.  A royalty-free license is granted      ****
  22.  **** by the company to use this patent for NON_COMMERCIAL PURPOSES to    ****
  23.  **** adapt logic trees using this program and its modifications.         ****
  24.  ****                                                                     ****
  25.  **** Limited Warranty:                                                   ****
  26.  **** This software is provided "as is" without warranty of any kind,     ****
  27.  **** either expressed or implied, including, but not limited to, the     ****
  28.  **** implied warrantees of merchantability and fitness for a particular  ****
  29.  **** purpose.  The entire risk as to the quality and performance of the  ****
  30.  **** program is with the user.  Neither the authors, nor the             ****
  31.  **** University of Alberta, its officers, agents, servants or employees  ****
  32.  **** shall be liable or responsible in any way for any damage to         ****
  33.  **** property or direct personal or consequential injury of any nature   ****
  34.  **** whatsoever that may be suffered or sustained by any licensee, user  ****
  35.  **** or any other party as a consequence of the use or disposition of    ****
  36.  **** this software.                                                      ****
  37.  **** Modification history:                                               ****
  38.  ****                                                                     ****
  39.  **** 92.04.27 atree v2.5 for Windows, M. Thomas                          ****
  40.  **** 92.03.07 Release 2.6, Monroe Thomas, Neal Sanche                    ****
  41.  **** 92.01.08 Release 2.7, Monroe Thomas, Neal Sanche                    ****
  42.  ****                                                                     ****
  43.  *****************************************************************************/
  44.  
  45. // bitmap.cpp
  46.  
  47. #include "bitmap.h"
  48.  
  49. const double TBitmap_PI = 3.14159265359;
  50.  
  51. void
  52. TBitmap::Init(HBITMAP hABitmap)
  53. {
  54.     BITMAP bm;
  55.     HDC hdc1, hdc2;
  56.  
  57.     if (hBitmap != NULL)
  58.     {
  59.         DeleteObject(hBitmap);
  60.         hBitmap = NULL;
  61.     }
  62.  
  63.     if (hABitmap == NULL)
  64.     {
  65.         hBitmap = NULL;
  66.         return;
  67.     }
  68.  
  69.     GetObject(hABitmap, sizeof(BITMAP), (LPSTR)&bm);
  70.     hBitmap = CreateBitmap(bm.bmWidth, bm.bmHeight, bm.bmPlanes,
  71.                                                  bm.bmBitsPixel, NULL);
  72.  
  73.     hdc1 = CreateCompatibleDC(NULL);
  74.     hdc2 = CreateCompatibleDC(NULL);
  75.  
  76.     SelectObject(hdc1, hABitmap);
  77.     SelectObject(hdc2, hBitmap);
  78.  
  79.     BitBlt(hdc2, 0, 0, bm.bmWidth, bm.bmHeight,
  80.                  hdc1, 0, 0, SRCCOPY);
  81.  
  82.     DeleteDC(hdc1);
  83.     DeleteDC(hdc2);
  84. }
  85.  
  86. DWORD
  87. TBitmap::GetBits(LPSTR lpBits)
  88. {
  89.     return GetBitmapBits(hBitmap, GetBytes(), lpBits);
  90. }
  91.  
  92. LONG
  93. TBitmap::SetBits(LPSTR lpBits, DWORD dwCount)
  94. {
  95.     return SetBitmapBits(hBitmap, dwCount, lpBits);
  96. }
  97.  
  98. DWORD
  99. TBitmap::GetWidth()
  100. {
  101.     BITMAP bm;
  102.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  103.     return bm.bmWidth;
  104. }
  105.  
  106. DWORD
  107. TBitmap::GetHeight()
  108. {
  109.     BITMAP bm;
  110.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  111.     return bm.bmHeight;
  112. }
  113.  
  114. DWORD
  115. TBitmap::GetPlanes()
  116. {
  117.     BITMAP bm;
  118.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  119.     return bm.bmPlanes;
  120. }
  121.  
  122. DWORD
  123. TBitmap::GetBitsPixel()
  124. {
  125.     BITMAP bm;
  126.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  127.     return bm.bmBitsPixel;
  128. }
  129.  
  130. DWORD
  131. TBitmap::GetWidthBytes()
  132. {
  133.     BITMAP bm;
  134.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  135.     return bm.bmWidthBytes;
  136. }
  137.  
  138. DWORD
  139. TBitmap::GetBytes()
  140. {
  141.     BITMAP bm;
  142.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  143.     return (bm.bmWidthBytes * bm.bmPlanes * bm.bmHeight);
  144. }
  145.  
  146. COLORREF
  147. TBitmap::GetDrawColor()
  148. {
  149.     return crDraw;
  150. }
  151.  
  152. COLORREF
  153. TBitmap::SetDrawColor(COLORREF crColor)
  154. {
  155.     COLORREF tmp = crDraw;
  156.     crDraw = crColor;
  157.     return tmp;
  158. }
  159.  
  160. COLORREF
  161. TBitmap::GetEraseColor()
  162. {
  163.     return crErase;
  164. }
  165.  
  166. COLORREF
  167. TBitmap::SetEraseColor(COLORREF crColor)
  168. {
  169.     COLORREF tmp = crErase;
  170.     crErase = crColor;
  171.     return tmp;
  172. }
  173.  
  174.  
  175. DWORD
  176. TBitmap::GetPoint(int x, int y)
  177. {
  178.     HDC hdc = CreateCompatibleDC(NULL);
  179.     DWORD retvalue;
  180.     SelectObject(hdc, hBitmap);
  181.     retvalue = GetPixel(hdc, x, y);
  182.     DeleteDC(hdc);
  183.     return retvalue;
  184. }
  185.  
  186. DWORD
  187. TBitmap::SetPoint(int x, int y, COLORREF crColor)
  188. {
  189.     HDC hdc = CreateCompatibleDC(NULL);
  190.     DWORD retvalue;
  191.     SetBkColor(hdc, crErase);
  192.     SetTextColor(hdc, crDraw);
  193.     SelectObject(hdc, hBitmap);
  194.     retvalue = SetPixel(hdc, x, y, crColor);
  195.     DeleteDC(hdc);
  196.     return retvalue;
  197. }
  198.  
  199. void
  200. TBitmap::Rotate(float theta, BOOL isRadians)
  201. {
  202.     HBITMAP hbm;
  203.     BITMAP bm;
  204.     HDC hdc1 = CreateCompatibleDC(NULL);
  205.     HDC hdc2 = CreateCompatibleDC(NULL);
  206.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  207.  
  208. //    HCURSOR hcursor = LoadCursor(NULL, IDC_WAIT);
  209. //    hcursor = SetCursor(hcursor);
  210.  
  211.     if (!isRadians)
  212.     {
  213.         theta = theta * TBitmap_PI / 180;
  214.     }
  215.  
  216.     int c1x, c1y, c2x, c2y;
  217.     double p1x, p1y;
  218.     int p2x, p2y;
  219.     long double c0, c1, c2, c3; // color info
  220.     double a;    // angle from c2 to p2
  221.     double r;    // radius from c2 to p2
  222.  
  223.     int n;        // biggest dimension
  224.  
  225.     c1x = c2x = ceil(bm.bmWidth / 2.0) - 1;
  226.     c1y = c2y = ceil(bm.bmHeight / 2.0) - 1;
  227.  
  228.     hbm = CreateBitmap(bm.bmWidth, bm.bmHeight, bm.bmPlanes,
  229.                                          bm.bmBitsPixel, NULL);
  230.  
  231.     SelectObject(hdc1, hBitmap);
  232.     SelectObject(hdc2, hbm);
  233.  
  234.     n = (bm.bmWidth > bm.bmHeight) ? bm.bmWidth : bm.bmHeight;
  235.     n = n / 2;
  236.  
  237.     for(p2x = 0; p2x <= n; p2x++)
  238.     {
  239.         for(p2y = 0; p2y <= n; p2y++)
  240.         {
  241.             if (p2x == 0)
  242.             {
  243.                 a = TBitmap_PI / 2.0;
  244.             }
  245.             else
  246.             {
  247.                 a = atan2(p2y, p2x);
  248.             }
  249.             r = sqrt((DWORD)p2x*p2x + (DWORD)p2y*p2y);
  250.  
  251.             // compute rotated position p1
  252.             p1x = r * cos(a + theta);
  253.             p1y = r * sin(a + theta);
  254.  
  255.             // copy pixels, 4 quadrants at once
  256.  
  257.             c0 = GetPixel(hdc1, floor(c1x + p1x + .5), floor(c1y + p1y + .5));
  258.             c1 = GetPixel(hdc1, floor(c1x - p1x + .5), floor(c1y - p1y + .5));
  259.             c2 = GetPixel(hdc1, floor(c1x + p1y + .5), floor(c1y - p1x + .5));
  260.             c3 = GetPixel(hdc1, floor(c1x - p1y + .5), floor(c1y + p1x + .5));
  261.  
  262.             if (c0 != -1)
  263.             {
  264.                 SetPixel(hdc2, floor(c2x + p2x + .5), floor(c2y + p2y + .5), c0);
  265.             }
  266.             if (c1 != -1)
  267.             {
  268.                 SetPixel(hdc2, floor(c2x - p2x + .5), floor(c2y - p2y + .5), c1);
  269.             }
  270.             if (c2 != -1)
  271.             {
  272.                 SetPixel(hdc2, floor(c2x + p2y + .5), floor(c2y - p2x + .5), c2);
  273.             }
  274.             if (c3 != -1)
  275.             {
  276.                 SetPixel(hdc2, floor(c2x - p2y + .5), floor(c2y + p2x + .5), c3);
  277.             }
  278.         }    // for p2y
  279.  
  280.         MSG msg;
  281.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  282.         {
  283.             TranslateMessage(&msg);
  284.             DispatchMessage(&msg);
  285.         }
  286.  
  287.     } // for p2x
  288.  
  289.     BitBlt(hdc1, 0, 0, bm.bmWidth, bm.bmHeight,
  290.                  hdc2, 0, 0, SRCCOPY);
  291.  
  292.     DeleteDC(hdc2);
  293.     DeleteDC(hdc1);
  294.     DeleteObject(hbm);
  295. //    SetCursor(hcursor);
  296. }
  297.  
  298. void
  299. TBitmapControl::GetWindowClass(WNDCLASS &AWndclass)
  300. {
  301.     TControl::GetWindowClass(AWndclass);
  302.     AWndclass.hbrBackground = GRAY_BRUSH;
  303. }
  304.  
  305. void
  306. TBitmapControl::Paint(HDC PaintDC, PAINTSTRUCT _FAR & ps)
  307. {
  308.     HBITMAP hOldbm;
  309.     BITMAP bm;
  310.  
  311.     RECT rc;
  312.  
  313.     HDC hMemDC = CreateCompatibleDC(PaintDC);
  314.  
  315.     SetBkColor(PaintDC, crErase);
  316.     SetTextColor(PaintDC, crDraw);
  317.  
  318.     GetClientRect(HWindow, &rc);
  319.     hOldbm = SelectObject(hMemDC, hBitmap);
  320.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  321.     StretchBlt(PaintDC, 0, 0, rc.right,
  322.                      rc.bottom,
  323.                      hMemDC, 0, 0,
  324.                      bm.bmWidth,
  325.                      bm.bmHeight, SRCCOPY);
  326.     SelectObject(hMemDC, hOldbm);
  327.     DeleteDC(hMemDC);
  328. }
  329.  
  330. DWORD
  331. TBitmapControl::GetPoint(int x, int y)
  332. {
  333.     HDC hdc = GetDC(HWindow);
  334.     DWORD retvalue;
  335.     SelectObject(hdc, hBitmap);
  336.     retvalue = GetPixel(hdc, x, y);
  337.     ReleaseDC(HWindow, hdc);
  338.     return retvalue;
  339. }
  340.  
  341. DWORD
  342. TBitmapControl::SetPoint(int x, int y, COLORREF crColor)
  343. {
  344.     HDC hdc1 = GetDC(HWindow);
  345.     HDC hdc2 = CreateCompatibleDC(hdc1);
  346.     RECT rc;
  347.     BITMAP bm;
  348.     DWORD retvalue;
  349.  
  350.     SetBkColor(hdc1, crErase);
  351.     SetTextColor(hdc1, crDraw);
  352.     GetClientRect(HWindow, &rc);
  353.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  354.     SelectObject(hdc2, hBitmap);
  355.     retvalue = SetPixel(hdc2, x, y, crColor);
  356.     StretchBlt(hdc1, 0, 0, rc.right, rc.bottom,
  357.                          hdc2, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  358.     DeleteDC(hdc2);
  359.     ReleaseDC(HWindow, hdc1);
  360.     return retvalue;
  361. }
  362.  
  363. void
  364. TBitmapControl::Reset(short w, short h)
  365. {
  366.     HDC hdc;
  367.  
  368.     if(hBitmap != NULL)
  369.     {
  370.         DeleteObject(hBitmap);
  371.     }
  372.  
  373.     hdc = GetDC(HWindow);
  374.     hBitmap = CreateCompatibleBitmap(hdc, w, h);
  375.     ReleaseDC(HWindow,hdc);
  376. }
  377.  
  378.  
  379. void
  380. TBitmapDrawControl::GetWindowClass(WNDCLASS &AWndclass)
  381. {
  382.     TBitmapControl::GetWindowClass(AWndclass);
  383.     AWndclass.style |= CS_DBLCLKS;
  384.     AWndclass.hCursor = LoadCursor(GetApplication()->hInstance, "pencilcur");
  385. }
  386.  
  387. void
  388. TBitmapDrawControl::setdimensions(HDC wndDC)
  389. {
  390.     BITMAP bm;
  391.     RECT rc;
  392.     GetClientRect(HWindow, &rc);
  393.     SetMapMode(wndDC, MM_ANISOTROPIC);
  394.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  395.     SetWindowExt(wndDC, bm.bmWidth, bm.bmHeight);
  396.     SetViewportExt(wndDC, rc.right, rc.bottom);
  397.     SetViewportOrg(wndDC, 0, 0);
  398. }
  399.  
  400. void
  401. TBitmapDrawControl::WMLButtonDown(RTMessage Msg)
  402. {
  403.     TBitmapControl::WMLButtonDown(Msg);
  404.     POINT pt;
  405.     HDC wndDC = GetDC(HWindow);
  406.     pt.x = Msg.LP.Lo;
  407.     pt.y = Msg.LP.Hi;
  408.     setdimensions(wndDC);
  409.     DPtoLP(wndDC, &pt, 1);
  410.     ReleaseDC(HWindow, wndDC);
  411.     drawon = TRUE;
  412.     SetPoint(pt.x, pt.y, crDraw);
  413.     TBDCClear = FALSE;
  414.  
  415. }
  416.  
  417. void
  418. TBitmapDrawControl::WMLButtonUp(RTMessage Msg)
  419. {
  420.     POINT pt;
  421.     HDC wndDC = GetDC(HWindow);
  422.     pt.x = Msg.LP.Lo;
  423.     pt.y = Msg.LP.Hi;
  424.     setdimensions(wndDC);
  425.     DPtoLP(wndDC, &pt, 1);
  426.     ReleaseDC(HWindow, wndDC);
  427.     drawon = FALSE;
  428.     if (!TBDCClear)
  429.     {
  430.         SetPoint(pt.x, pt.y, crDraw);
  431.     }
  432.     TBDCClear = FALSE;
  433. }
  434.  
  435. void
  436. TBitmapDrawControl::WMLButtonDblClk(RTMessage Msg)
  437. {
  438.     RECT rc;
  439.     BITMAP bm;
  440.     HBRUSH hbr;
  441.  
  442.     hbr = GetStockObject(BLACK_BRUSH);
  443.  
  444.     HDC hdc1 = GetDC(HWindow);
  445.     HDC hdc2 = CreateCompatibleDC(hdc1);
  446.  
  447.     SetBkColor(hdc1, crErase);
  448.     SetTextColor(hdc1, crDraw);
  449.  
  450.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  451.     rc.left = rc. top = 0;
  452.     rc.right = bm.bmWidth;
  453.     rc.bottom = bm.bmHeight;
  454.     SelectObject(hdc2, hBitmap);
  455.     FillRect(hdc2, &rc, hbr);
  456.  
  457.     GetClientRect(HWindow, &rc);
  458.  
  459.     StretchBlt(hdc1, 0, 0, rc.right, rc.bottom,
  460.                          hdc2, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  461.     DeleteDC(hdc2);
  462.     ReleaseDC(HWindow, hdc1);
  463.     TBDCClear = TRUE;
  464. }
  465.  
  466. void
  467. TBitmapDrawControl::WMRButtonDown(RTMessage Msg)
  468. {
  469.     POINT pt;
  470.     HDC wndDC = GetDC(HWindow);
  471.     pt.x = Msg.LP.Lo;
  472.     pt.y = Msg.LP.Hi;
  473.     setdimensions(wndDC);
  474.     DPtoLP(wndDC, &pt, 1);
  475.     ReleaseDC(HWindow, wndDC);
  476.     eraseon = TRUE;
  477.     SetPoint(pt.x, pt.y, crErase);
  478.     TBDCRotate = FALSE;
  479. }
  480.  
  481. void
  482. TBitmapDrawControl::WMRButtonUp(RTMessage Msg)
  483. {
  484.     POINT pt;
  485.     HDC wndDC = GetDC(HWindow);
  486.     pt.x = Msg.LP.Lo;
  487.     pt.y = Msg.LP.Hi;
  488.     setdimensions(wndDC);
  489.     DPtoLP(wndDC, &pt, 1);
  490.     ReleaseDC(HWindow, wndDC);
  491.     eraseon = FALSE;
  492.     if (!TBDCRotate)
  493.     {
  494.         SetPoint(pt.x, pt.y, crErase);
  495.     }
  496.     else
  497.     {
  498.         Rotate(5);
  499.         TBDCRotate = FALSE;
  500.         Refresh();
  501.     }
  502. }
  503.  
  504. void
  505. TBitmapDrawControl::WMRButtonDblClk(RTMessage Msg)
  506. {
  507.     RECT rc;
  508.     BITMAP bm;
  509.     HBRUSH hbr;
  510.  
  511.     hbr = GetStockObject(WHITE_BRUSH);
  512.  
  513.     HDC hdc1 = GetDC(HWindow);
  514.     HDC hdc2 = CreateCompatibleDC(hdc1);
  515.  
  516.     SetBkColor(hdc1, crErase);
  517.     SetTextColor(hdc1, crDraw);
  518.  
  519.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  520.     rc.left = rc. top = 0;
  521.     rc.right = bm.bmWidth;
  522.     rc.bottom = bm.bmHeight;
  523.     SelectObject(hdc2, hBitmap);
  524.     FillRect(hdc2, &rc, hbr);
  525.  
  526.     GetClientRect(HWindow, &rc);
  527.     StretchBlt(hdc1, 0, 0, rc.right, rc.bottom,
  528.                          hdc2, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
  529.     DeleteDC(hdc2);
  530.     ReleaseDC(HWindow, hdc1);
  531. }
  532.  
  533. void
  534. TBitmapDrawControl::WMMouseMove(RTMessage Msg)
  535. {
  536.     POINT pt;
  537.     HDC wndDC = GetDC(HWindow);
  538.     pt.x = Msg.LP.Lo;
  539.     pt.y = Msg.LP.Hi;
  540.     setdimensions(wndDC);
  541.     DPtoLP(wndDC, &pt, 1);
  542.     SendMessage(Parent->HWindow, TBDC_COORDCHANGE, 0, MAKELONG(pt.x, pt.y));
  543.     ReleaseDC(HWindow, wndDC);
  544.     if (eraseon) SetPoint(pt.x, pt.y, RGB(255, 255, 255));
  545.     else if (drawon) SetPoint(pt.x, pt.y, RGB(0, 0, 0));
  546. }